package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.CompletionException;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public List<Address> findList() {
        //创建条件
        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(Address::getUserId, UserHolder.get().getId());
        //执行查询
        List<Address> addresses = addressMapper.selectList(addressLambdaQueryWrapper);

        return addresses;
    }

    @Override
    public void save(Address address) {
        //设置用户id
        address.setUserId(UserHolder.get().getId());

        //保存
        addressMapper.insert(address);
    }

    @Override
    public void setDefault(Long id) {
        //修改当前用户所有地址的is_default=0
        //封装要更新的字段
        Address address = new Address();
        address.setIsDefault(0);

        //设置更新条件
        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(Address::getUserId, UserHolder.get().getId());

        //执行方法
        addressMapper.update(address, addressLambdaQueryWrapper);

        //将指定的id记录
        Address address1 = new Address();
        address1.setId(id);
        address1.setIsDefault(1);
        addressMapper.updateById(address1);
    }

    @Override
    public Address findDefault() {
        //创建条件
        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(Address::getUserId, UserHolder.get().getId());
        addressLambdaQueryWrapper.eq(Address::getIsDefault, 1);
        //执行查询
        return addressMapper.selectOne(addressLambdaQueryWrapper);
    }

    @Override
    public Address findById(Long id) {
        LambdaQueryWrapper<Address> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper.eq(Address::getId, id);
        //查询
        Address address = addressMapper.selectOne(orderLambdaQueryWrapper);

        return address;
    }

    @Override
    public void updateAddress(Address address) {
        //根据id修改
//        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        addressLambdaQueryWrapper.eq(Address::getId, address.getId());
        addressMapper.updateById(address);

    }

    @Override
    public void deleteAddress(Long[] ids) {
        for (Long id : ids) {
            Address address = addressMapper.selectById(id);
            if (address.getIsDefault() == 1) {
                throw new CustomException("该地址为默认地址，不能删除");
            }
            addressMapper.deleteById(id);
        }
    }
}
